package selcoursesys.service.impl;

import com.opensymphony.xwork2.ActionContext;
import selcoursesys.dao.CourseDAO;
import selcoursesys.dao.SelectionDAO;
import selcoursesys.dao.StudentDAO;
import selcoursesys.dao.impl.CourseDAOImpo;
import selcoursesys.dao.impl.SelectionDAOImpl;
import selcoursesys.dao.impl.StudentDAOImpl;
import selcoursesys.entity.*;
import selcoursesys.service.TeacherService;

import java.util.*;

/**
 * Created by Lpbdz on 2017/4/12.
 */
public class TeacherServiceImpl implements TeacherService {
    //接受教师id,得到教师所授课程的详细情况
    public boolean getInsInfo(String teaid){
        //查看课程信息表，得到教师所授课程
        /**
         * course  tid,cid,ctime,cclass,lnum,cnum
         * */
        List<CourseInfo> courseInfos = new LinkedList<CourseInfo>();
        List<StudentInfo> studentInfos=new LinkedList<StudentInfo>();
        CourseDAO courseDAO=new CourseDAOImpo();
        CourseEntity courseEntity=courseDAO.queryCourseTid(teaid);
        if(courseEntity==null){
            return false;
        }
        String courseid =courseEntity.getCourseid();
        //封装课程相关信息
        String cday=GetClassTime.getClassTime(courseEntity.getCoursetime());
        String descrice="第"+courseEntity.getBweek()+"周到第"+courseEntity.getEweek()+"周"+cday;
        courseInfos.add(new  CourseInfo(courseEntity.getCoursename(),courseEntity.getCredit(),courseEntity.getLnum(),courseEntity.getCnum(),courseEntity.getClasshour(),courseEntity.getRid(),descrice));
        ActionContext.getContext().put("classInfo",courseInfos);

        SelectionDAO selectionDAO=new SelectionDAOImpl();
        //根据课程id,查看学生id
        List <SelectionEntity> selectionEntities=selectionDAO.querySelectCid(courseid);

        Iterator iterstuid=selectionEntities.iterator();
        while(iterstuid.hasNext()){
            SelectionEntity selectionEntity=(SelectionEntity)iterstuid.next();
            StudentDAO studentDAO=new StudentDAOImpl();
            String stuid=selectionEntity.getStuid();
            System.out.println(stuid);
            //根据学号GET学生信息
            List<StudentEntity> liststu=studentDAO.queryStudentId(stuid);
            Iterator iterstuinfo=liststu.iterator();
            while(iterstuinfo.hasNext()){
                StudentEntity studentEntity=(StudentEntity)iterstuinfo.next();
                studentInfos.add(new StudentInfo(studentEntity.getStuid(),studentEntity.getStuname() ,studentEntity.getAcademyByAid().getAname(),studentEntity.getClazzByClassid().getClassname()));
            }
        }
        SaveFile.saveFile(studentInfos);
        ActionContext.getContext().put("stuinfo",studentInfos);
        return true;
    }
}
